<?php
if (! defined('IN_WEB')) {
    exit('Access Denied');
}

class TaxonomyAction {

    function __construct () {
        $this->vocabulary = new DbKooltaoModel('kooltao_vocabulary');
        $this->term = new DbKooltaoModel('kooltao_term');
        $this->term_data = new DbKooltaoModel('kooltao_term_data');
    }

    function vocabularyAdd ($data) {
        $data['name'] = trim($data['name']);
        $data['machine_name'] = trim($data['machine_name']);
        $data['description'] = trim($data['description']);
        $data['weight'] = intval($data['weight']);
        
        $where[] = array(
            'machine_name', $data['machine_name']
        );
        if ($this->vocabulary->getOne($where)) {
            return - 2;
        }
        return $this->vocabulary->insert($data) ? 1 : - 1;
    }

    function termUpdate($data){
        return $this->term->update($data);
    }
    function vocabularyUpdate ($data) {
        $data['vid'] = intval($data['vid']);
        $data['name'] = trim($data['name']);
        $data['machine_name'] = trim($data['machine_name']);
        $data['description'] = trim($data['description']);
        $data['weight'] = intval($data['weight']);
        $where[] = array(
            'vid', $data['vid'], '<>'
        );
        $where[] = array(
            'machine_name', $data['machine_name']
        );
        if ($this->vocabulary->getOne($where)) {
            return - 2;
        }
        return $this->vocabulary->update($data) ? 1 : - 1;
    }

    function vocabularyList () {
        return $this->vocabulary->getList(null, null, 0, 0, 'weight');
    }

    function vocabularyListJson () {
        $list = $this->vocabularyList();
        $count = $this->vocabulary->count();
        $json_arr = array(
            
            'sEcho' => intval($_GET['sEcho']), 
            'iTotalRecords' => $count, 
            "iTotalDisplayRecords" => $count
        );
        $aaData = array();
        foreach ($list as $v) {
            $aaData[] = array(
                
                $v['vid'], 
                $v['name'], 
                $v['machine_name'], 
                "<a href='javascript:void(0)' onclick='edit_voc({$v['vid']})'>编辑</a>	 |
				<a href='javascript:void(0)' onclick='del_voc({$v['vid']})'>删除</a>", 
                "<a href='javascript:void(0)' onclick='term_list({$v['vid']})'>查看术语</a> | 
				<a href='javascript:void(0)' onclick='term_add({$v['vid']})'>添加术语</a>"
            );
        }
        $json_arr['aaData'] = $aaData;
        return json_encode($json_arr);
    }

    function vocabularyGetOne ($vid) {
        return $this->vocabulary->getOneById($vid);
    }

    function vocabularyDel ($vid) {
        $terms = $this->getTerms($vid);
        if ($terms) {
            return - 2;
        }
        return $this->vocabulary->deleteById($vid) ? '1' : '-1';
    }

    function termAdd ($data) {
        $data['vid'] = intval($data['vid']);
        $data['name'] = trim($data['name']);
        $data['parent'] = intval($data['parent']);
        $data['description'] = trim($data['description']);
        $data['weight'] = intval($data['weight']);
        
        return $this->term->insert($data) ? 1 : - 1;
    }

    function termList ($vid, $parent = null) {
        if (! ($parent === null))
            $where[] = array(
                'parent', $parent
            );
        $where[] = array(
            'vid', $vid
        );
        return $this->term->getList(null, $where,'','','weight ASC, tid ASC');
    }

    function termTree($vid, $parent=0, $level=0){
        $data = array();
        $parent = intval($parent);
        static $list;
        if(empty($list)){
            $list = $this->termList($vid);
        }

        
        $levelNext = $level+1;
        foreach($list as $term){
            if($term['parent'] == $parent){
                $term['level'] = $level;
                $term['preWidth'] = $level*25;
                $data[] = $term;
                $children = $this->termTree($vid, $term['tid'], $levelNext);
                foreach($children as $cTerm){
                    $data[] = $cTerm;
                }
            }
        }
        
        return $data;
    }

    function termListJson ($vid, $parent = null) {
        $list = $this->termTree($vid, $parent);
        $count = count($list);
        $json_arr = array(
            
            'sEcho' => intval($_GET['sEcho']), 
            'iTotalRecords' => $count, 
            "iTotalDisplayRecords" => $count
        );
        $aaData = array();
        foreach ($list as $t) {
            $aaData[] = array(
                
                $t['preLevel'].$t['tid'], 
                $t['name'], 
                "<a href='javascript:void(0)' onclick='term_list_by_parent({$t['vid']}, {$t['tid']})'>查看术语</a> | "
                ."<a href='javascript:void(0)' onclick='term_add_by_parent({$t['vid']}, {$t['tid']})'>添加术语</a> | "
                ."<a href='javascript:void(0)' onclick='del_term({$t['tid']})'>删除</a>"
            );
        }
        $json_arr['aaData'] = $aaData;
        return json_encode($json_arr);
    }

    function getTerm ($tid) {
        return $this->term->getOneById($tid);
    }

    function termDel ($tid) {
        $term = $this->getTerm($tid);
        if (! $term) {
            return - 2;
        }
        return $this->term->deleteById($tid) ? '1' : '-1';
    }

    function getVocabularyByMachineName ($mn) {
        $where[] = array(
            'machine_name', $mn
        );
        return $this->vocabulary->getOne($where);
    }

    function getColorTerms () {
        $voc = $this->getVocabularyByMachineName(TERM_MACHINE_NAME_COLOR);
        return $this->termList($voc['vid']);
    }

    function getSourceTerms () {
        $voc = $this->getVocabularyByMachineName(TERM_MACHINE_NAME_SOURCE);
        return $this->termList($voc['vid']);
    }

    function getPtypeFirst () {
        $voc = $this->getVocabularyByMachineName(TERM_MACHINE_NAME_PTYPE);
        return $this->termList($voc['vid'], 0);
    }
    //查询下级菜单数据（Tags数据）
    function termTagsList ($parent = null) {
        if (! ($parent === null))
            $where[] = array(
                'parent', $parent
            );
        return $this->term->getList(null, $where);
    }
    //增加Tag数据的现实
    function getPtypeTags ($parent) {
        $voc = $this->getVocabularyByMachineName(TERM_MACHINE_NAME_PTYPE);
        return $this->termTagsList($parent);
    }

    function termListByTids ($tids) {
        is_array($tids) && $tids = array_filter($tids);
        if ($tids) {
            $where = 'tid IN (' . implode(', ', $tids) . ')';
            return $this->term->getList(null, $where);
        }
        return null;
    
    }

    function termDataAdd ($tids, $pid) {
        $terms = $this->termListByTids($tids);
        $data = array();
        if ($terms) {
            foreach ($terms as $term) {
                $row = array(
                    
                'tid' => $term['tid'], 'vid' => $term['vid'], 'pid' => $pid
                );
                $data[] = $row;
            }
            return $this->term_data->batchInsert($data);
        }
    }

    
    function termDataDeleteByPid ($pid) {
        $where[] = array(
            'pid', $pid
        );
        $this->term_data->delete($where);
    }
}